const child_process = require("child_process");
const fs = require("fs");
const path = require("path");

var keyDir = path.join(__dirname, "hfc-key-store");
fs.rmdirSync(keyDir, {
  recursive: true,
});
console.log("clear dir " + keyDir);
fs.mkdirSync(keyDir);
console.log("make dir " + keyDir);

child_process.execFileSync("node", ["enrollAdmin.js"]);
console.log("run enrollAdmin.js successeful");

child_process.execFileSync("node", ["registerUser.js"]);
console.log("run registerUser.js successeful");

console.log("node  app.js");
// child_process.execFile("node", ["app.js"], (error, stdout, stderr) => {
//   if (error) {
//     throw error;
//   }
//   console.log(stdout);
// });

const app = child_process.spawn('node', ['app.js']);
app.stdout.on("data", (data) => {
  console.log(`stdout: ${data}`);
});

app.stderr.on("data", (data) => {
  console.error(`stderr: ${data}`);
});

app.on("close", (code) => {
  console.log(`child process exited with code ${code}`);
});
